﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Recursos.Model
{
    public class ComentariosModel
    {
        public string ObtenerComentariosReservacion(int id)
        {
            string comentarios = "";

            var datacontext = new DataClassesRecursosDataContext();
            var repoComentarios = new Repository<Comentario>(datacontext);
            int idRes;

            var resCursos = new Repository<ReservacionCurso>(datacontext);
            var reservacion = resCursos.GetById(id);
            idRes = reservacion.IdReservacion;

            var comentariosAsoc = repoComentarios.SearchFor(com => com.IdReservacion == idRes && com.Borrado == false);
            foreach (Comentario com in comentariosAsoc)
            {
                comentarios += "\n ---------------- \n";
                comentarios += com.Owner + "\t" + com.Fecha.ToShortDateString() + "\n";
                comentarios += com.Comentario1 + "\n";
            }

            return comentarios;
        }

        public string ObtenerComentariosReservacionParticular(int id)
        {
            string comentarios = "";

            var datacontext = new DataClassesRecursosDataContext();
            var repoComentarios = new Repository<Comentario>(datacontext);
            int idRes;

            var resParticulares = new Repository<ReservacionParticular>(datacontext);
            var reservacion = resParticulares.GetById(id);
            idRes = reservacion.IdReservacion;

            var comentariosAsoc = repoComentarios.SearchFor(com => com.IdReservacion == idRes && com.Borrado == false);
            foreach (Comentario com in comentariosAsoc)
            {
                comentarios += "\n ---------------- \n";
                comentarios += com.Owner + "\t" + com.Fecha.ToShortDateString() + "\n";
                comentarios += com.Comentario1 + "\n";
            }

            return comentarios;
        }

        public int AgregarComentario(int idReservacion, string comentario, DateTime fecha, string owner)
        {

            var datacontext = new DataClassesRecursosDataContext();
            var repoComentarios = new Repository<Comentario>(datacontext);

            var resCursos = new Repository<ReservacionCurso>(datacontext);
            var reservacion = resCursos.GetById(idReservacion);
            int idRes = reservacion.IdReservacion;

            var nvoComentario = new Comentario { IdReservacion = idRes, Comentario1 = comentario, Fecha = fecha, Owner = owner, Borrado = false };
            repoComentarios.Insert(nvoComentario);
            datacontext.SubmitChanges();
            return nvoComentario.id;
        }

        public int AgregarComentarioParticular(int idReservacion, string comentario, DateTime fecha, string owner)
        {

            var datacontext = new DataClassesRecursosDataContext();
            var repoComentarios = new Repository<Comentario>(datacontext);

            var resCursos = new Repository<ReservacionParticular>(datacontext);
            var reservacion = resCursos.GetById(idReservacion);
            int idRes = reservacion.IdReservacion;

            var nvoComentario = new Comentario { IdReservacion = idRes, Comentario1 = comentario, Fecha = fecha, Owner = owner, Borrado = false };
            repoComentarios.Insert(nvoComentario);
            datacontext.SubmitChanges();
            return nvoComentario.id;
        }
    }
}